package com.fps.web.filter;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.fps.web.model.BaseUser;
import com.fps.web.model.BaseUserLog;
import com.fps.web.service.BaseUserLogService;
import com.fpswork.core.exception.exception.BusinessException;
import com.fpswork.util.DateUtils;


public class WebappSessionFilter implements Filter {

	

	// 设置例外的URI请求
	private static String[] OUT_OF_FILTER_URIS = { "/login.html", "/loginSubmit.html"};



	public void init(FilterConfig filterConfig) throws ServletException {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {		
		HttpServletRequest httpReq = (HttpServletRequest) request;
		HttpSession httpSession = httpReq.getSession();
		String requestUri = httpReq.getServletPath();
		for (int i = 0; i < OUT_OF_FILTER_URIS.length; i++) {
			if (OUT_OF_FILTER_URIS[i].equals(requestUri)) {
				chain.doFilter(request, response);
				return;
			}
			//如果是手机端接口，就放行
			if(requestUri.startsWith("/m_app") ){
				chain.doFilter(request, response);
				return;
			}
			//所有的外部开放接口都从这个端口进行进出
			if(requestUri.startsWith("/openInterface") ){
				chain.doFilter(request, response);
				return;
			}
		}
		if (httpSession.getAttribute("LOGIN_SYS_USER_INFO") == null) {
			PrintWriter out = response.getWriter();  
	        out.println("<html>");      
	        out.println("<script>");      
	        out.println("window.open ('"+httpReq.getContextPath() + "/login.html','_top')");      
	        out.println("</script>");      
	        out.println("</html>");
			/*httpRes.sendRedirect(httpReq.getContextPath() + "/login.html");*/
		} else {
			chain.doFilter(request, response);
		}
	}

	public void destroy() {
	}
}
